package com.ln.lnzw.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.ln.lnzw.R;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.utils.CacheUtils;
import com.ln.lnzw.utils.ToastFactory;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wuwei on 2018/3/29.
 * 设置
 */

public class SettingsActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_my_set_zhiliao)
    RelativeLayout rlMySetZhiliao;
    @BindView(R.id.rl_my_set_guanyuwomen)
    RelativeLayout rlMySetGuanyuwomen;
    @BindView(R.id.rl_my_set_fankui)
    RelativeLayout rlMySetFankui;
    @BindView(R.id.rl_my_set_huancun)
    RelativeLayout rlMySetHuancun;
    @BindView(R.id.tv_huancun)
    TextView tvHuancun;
    @BindView(R.id.rl_tuichudenglu)
    RelativeLayout rlTuichudenglu;
    @BindView(R.id.rl_my_set_xiugaimima)
    RelativeLayout rlMySetXiugaimima;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        ButterKnife.bind(this);
        if (!activity.spUtils.getString(AppConstant.TOKEN).isEmpty()) {
            rlTuichudenglu.setVisibility(View.VISIBLE);
        } else {
            rlTuichudenglu.setVisibility(View.GONE);
        }
        try {
            tvHuancun.setText(CacheUtils.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.iv_back, R.id.rl_my_set_zhiliao, R.id.rl_my_set_guanyuwomen, R.id.rl_my_set_fankui, R.id.rl_my_set_huancun,R.id.rl_tuichudenglu,R.id.rl_my_set_xiugaimima})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_my_set_zhiliao:
                if (!activity.spUtils.getString(AppConstant.TOKEN).isEmpty()) {
                    ActivityUtils.startActivity(activity, PersonalInformationSettingsActivity.class);
                } else {
                    ToastFactory.getToast(activity, "请登录").show();
                }
                break;
            case R.id.rl_my_set_guanyuwomen:
                ActivityUtils.startActivity(activity, AboutUsActivity.class);
                break;
            case R.id.rl_my_set_fankui:
                ActivityUtils.startActivity(activity, FeedbackActivity.class);
                break;
            case R.id.rl_my_set_huancun:
                clearCache();
                break;
            case R.id.rl_tuichudenglu:
                DropOutLogin();
                break;
            case R.id.rl_my_set_xiugaimima:
                ActivityUtils.startActivity(activity, ChangePasswordActivity.class);
                break;
        }
    }
    //退出登录
    private void DropOutLogin() {
//        activity.spUtils.put(AppConstant.TOKEN,"");
//        activity.spUtils.put(AppConstant.USER_NAME,"");
//        activity.spUtils.put(AppConstant.USER_PIC,"");
//
//
//        activity.spUtils.put(AppConstant.USER_UID,"");
//        activity.spUtils.put(AppConstant.USER_CREDITID,"");
//        activity.spUtils.put(AppConstant.TRUE_NAME,"");
//        activity.spUtils.put(AppConstant.MOBILE,"");
//        activity.spUtils.put(AppConstant.EMAIL,"");
//        activity.spUtils.put(AppConstant.USER_PASSWORD,"");
//        activity.spUtils.put(AppConstant.LOGINACCOUNTNAME,"");
//        activity.spUtils.put(AppConstant.PASSWORD,"");
//        activity.spUtils.put(AppConstant.HEADURL,"");
//        activity.spUtils.put(AppConstant.USER_TRUE_NAME_STATE,"");
        activity.spUtils.remove(AppConstant.TOKEN);
        activity.spUtils.remove(AppConstant.USER_NAME);
        activity.spUtils.remove(AppConstant.USER_PIC);
        activity.spUtils.remove(AppConstant.USER_UID);
        activity.spUtils.remove(AppConstant.USER_CREDITID);
        activity.spUtils.remove(AppConstant.TRUE_NAME);
        activity.spUtils.remove(AppConstant.MOBILE);
        activity.spUtils.remove(AppConstant.EMAIL);
        activity.spUtils.remove(AppConstant.USER_PASSWORD);
        activity.spUtils.remove(AppConstant.LOGINACCOUNTNAME);
        activity.spUtils.remove(AppConstant.PASSWORD);
        activity.spUtils.remove(AppConstant.HEADURL);
        activity.spUtils.remove(AppConstant.USER_TRUE_NAME_STATE);
        EventBus.getDefault().post("change");

        finish();
    }

    /**
     * 清除缓存
     */
    private void clearCache() {
        CacheUtils.clearAllCache(this);
        tvHuancun.setText(getString(R.string.cache_num, 0.0));
        ToastFactory.getToast(activity, "清除成功").show();
    }

}
